/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.comm.message.impl;

import java.io.Serializable;

import org.kopsox.comm.message.Message;
import org.kopsox.comm.message.ValueMessage;
import org.kopsox.data.Value;
import org.kopsox.general.priority.Priority;
import org.kopsox.general.priority.PriorityEnum;
import org.kopsox.general.type.MessageType;
import org.kopsox.general.type.MessageTypeEnum;
import org.kopsox.general.type.ValueType;
import org.kopsox.general.type.ValueTypeEnum;

/**
 * Standardimplementation of the ValueMessage interface
 * 
 * @author Konrad Renner
 * 
 */
public class ValueMessageImpl implements ValueMessage {
	private static final long serialVersionUID = -5997827167846306078L;
	private final Value<Serializable> value;
	private Serializable oldValue;
	private ValueType typeFromOldValue;
	private transient Object messageSender;
	private Priority priority;
	private String namespace;

	public ValueMessageImpl(Value<Serializable> value) {
		this.value = value;
		this.messageSender = null;
		this.priority = PriorityEnum.MEDIUM.getPriority();
		this.namespace = null;
		this.oldValue = null;
	}

	@SuppressWarnings("unchecked")
	public static <T extends Serializable> ValueMessageImpl createValueMessage(Value<T> value) {
		return new ValueMessageImpl((Value<Serializable>) value);
	}

	@Override
	public Value<Serializable> getMessageValue() {
		return this.value;
	}

	@Override
	public Serializable getOldValue() {
		return this.oldValue;
	}

	@Override
	public Object getMessageSender() {
		return this.messageSender;
	}

	@Override
	public String getNameSpace() {
		return this.namespace;
	}

	@Override
	public void setMessageSender(Object sender) {
		this.messageSender = sender;
	}

	@Override
	public MessageType getType() {
		return MessageTypeEnum.VALUE;
	}

	@Override
	public Priority getPriority() {
		return this.priority;
	}

	@Override
	public void setPriority(Priority priority) {
		this.priority = priority;
	}

	public void setOldValue(Serializable oldValue) {
		this.oldValue = oldValue;
	}
	
	

	@Override
	public ValueType getTypeFromOldValue() {
		if(this.typeFromOldValue == null){
			this.typeFromOldValue = ValueTypeEnum.getTypeFromValue(getOldValue());
		}
		return this.typeFromOldValue;
	}
	

	public void setTypeFromOldValue(ValueType typeFromOldValue) {
		this.typeFromOldValue = typeFromOldValue;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((this.namespace == null) ? 0 : namespace.hashCode());
		result = prime * result
				+ ((this.oldValue == null) ? 0 : oldValue.hashCode());
		result = prime * result
				+ ((this.priority == null) ? 0 : priority.hashCode());
		result = prime * result + ((this.value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ValueMessageImpl other = (ValueMessageImpl) obj;
		if (this.namespace == null) {
			if (other.namespace != null)
				return false;
		} else if (!this.namespace.equals(other.namespace))
			return false;
		if (this.oldValue == null) {
			if (other.oldValue != null)
				return false;
		} else if (!this.oldValue.equals(other.oldValue))
			return false;
		if (this.priority == null) {
			if (other.priority != null)
				return false;
		} else if (!this.priority.equals(other.priority))
			return false;
		if (this.value == null) {
			if (other.value != null)
				return false;
		} else if (!this.value.equals(other.value))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("ValueMessageImpl:");
		sb.append("value=");
		sb.append(this.value.toString());
		sb.append(",oldValue=");
		sb.append(this.oldValue);
		sb.append(",namespace=");
		sb.append(this.namespace);
		sb.append(",priority=");
		sb.append(this.priority.toString());
		sb.append(",messageSender=");
		sb.append(this.messageSender);
		sb.append(";");
		return sb.toString();
	}

	@Override
	public int compareTo(Message o) {
		int erg = this.priority.compareTo(o.getPriority());
		if(erg == 0) {
			erg = this.getType().toString().compareTo(o.getType().toString());
			
			if(erg == 0) {
				erg = this.getClass().getName().compareTo(o.getClass().getName());
			}
		}
		return erg;
	}
}
